var app = getApp()
import Dialog from '../../../dist/dialog/dialog';
var location = require("../../../mybehaviors/location/location");

Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    domainName: "领域名称",
    option2: [{
        text: "由近到远",
        value: 1
      },
      {
        text: "由远到近",
        value: 2
      }
    ],
    categoryList: [],
    text: "距离",
    form: {
      distance_type: "",
      name: "",
      lng: "",
      lat: "",
      category_id: ""
    },
    point: [],
    myLocation: {},
    // listData: [],
    isLoadMore: true,
    page: 1,
    last_page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  onSearch(e) {
    this.setData({
      "form.name": e.detail
    })
    if (this.timerId) {
      clearTimeout(this.timerId)
    }
    this.timerId = setTimeout(() => {
      this.getSearch()
    }, 300)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCategory();
    this.getSearch()
  },
  getSearch() {
    let address = wx.getStorageSync('address-point')
    if(address && address.point){
      this.data.form.lng = address.point.lng;
      this.data.form.lat = address.point.lat;
      if (this.data.text !== "距离") {
        this.data.form.distance_type =  this.data.text == "由近到远" ? 1 : 2;
      }else if(this.data.text =='距离'){
        this.data.form.distance_type = 1;
      }
      app._postNetWork({
        url: app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.lawyer.getLawyerList"),
        data: {
          ...this.data.form
        },
        success: res => {
          if (res.data.result == 1) {
            this.setData({
              lawyer_info: res.data.data.list,
              page: res.data.data.list.current_page,
              last_page: res.data.data.list.last_page
            })
          }
        }
      })
    }else{
      Dialog.confirm({
        title: '提示',
        message: '定位失败,是否跳转手动定位',
      })
        .then(() => {
          wx.navigateTo({
            url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=suppligoodsOrde',
          })
        })
        .catch(() => {
          // on cancel
        })
   
    }
  
  },
  getCategory() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.lawyer.getCategory"),
      success: res => {
        if (res.data.result == 1) {
          this.setData({
            categoryList: res.data.data.category
          })
        }
      }
    })
  },
  domain(e) {
    let title = e.currentTarget.dataset.item.title,
      id = e.currentTarget.dataset.item.id;
    if (this.data.domainName == title) {
      this.setData({
        domainName: "领域名称",
        "form.category_id": ""
      })
    } else {
      this.setData({
        domainName: title,
        "form.category_id": id,
      })
    }
    this.selectComponent("#dropdown").toggle()
    this.getSearch()
  },
  change(e) {
    let text = e.currentTarget.dataset.item.text,
      distance_type = e.currentTarget.dataset.item.value;
    if (text == this.data.text) {
      this.setData({
        text: "距离",
        distance_type: ''
      })
      this.getSearch()
    } else {
      this.setData({text,distance_type});
      if (this.data.form.lng && this.data.form.lat) {
        this.getSearch();
      } else {
        this._getLocation((mapdata, e) => {
          this.setData({
            'form.lng': e.lng,
            'form.lat': e.lat,
          });
          this.getSearch();
        });
      }
    }
    this.selectComponent("#distance").toggle()


  },
  to(e){
    let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageI/lawyerPlatform/detail/detail?type_name=lawyer&id='+id,
      })
   
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page > this.data.last_page) {
      let json = {}
      if (this.data.form.name) {
        json.name = this.data.form.name
      };
      let urlStr = app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.lawyer.getLawyerList");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (res) => {
          if (res.data.result == 1) {
            this.data.lawyer_info.data.concat(res.data.data.list.data);
            this.setData({
              lawyer_info: this.data.lawyer_info,
              page: res.data.data.list.current_page,
              last_page: res.data.data.list.last_page
            })
          } else {
            app.tips(res.data.msg)
          }
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})